package PhotoController;


import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import common.PagingManager;

/**
 * Servlet implementation class PhotoFreeAction
 */
public class PhotoFreeAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
	PhotoFreeDAO photoFreeDAO=new PhotoFreeDAO();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PhotoFreeAction() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	private int currentPage=1;
	
 	private PagingManager paging;


	
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		PhotoFreeBean photoFreeBean= null;
		PhotoCommentBean photoCommentBean=null;
		RequestDispatcher view = null;
		PrintWriter out = response.getWriter();
		if(action.equals("pt_free_list_main")){
			System.out.println("포토");
			ArrayList list =photoFreeDAO.pt_free_recom();
			request.setAttribute("list", list);
			
			request.getRequestDispatcher("/Main/OldMain/home_photo3.jsp").forward(request, response);
		}else if(action.equals("pt_free_list2")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			
			String searchword=request.getParameter("searchword");
			String search=request.getParameter("search");
			//ArrayList list2 =photoFreeDAO.pt_free_recom();
			//ArrayList list3 =photoFreeDAO.pt_free_bestview();
			photoFreeBean=photoFreeDAO.pt_free_recom_top();
			if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			param.put("pageSize", 16 );
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
			int endNum = pageNo + (Integer)param.get("pageSize");
			param.put( "startNum", pageNo );
			param.put("searchword", searchword);
			param.put("search",search);
			//param.put( "recordCount", (Integer)param.get("pageSize") );
			ArrayList list =photoFreeDAO.pt_free_list(param);
			int totalCount = photoFreeDAO.pfTotalCount(param);
			param.put("totalCount", totalCount);
			paging=new common.PagingManager();
			paging.setPageNo(param);
			
			session = request.getSession();
			session.setAttribute("list", list);
			//session.setAttribute("list2", list2);
			//session.setAttribute("list3", list3);
			session.setAttribute("photoFreeBean", photoFreeBean);
			session.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page","/photo/PtFree/PT_Free_List2");
			
			if(level==2){
				session.setAttribute("photo", "free_photo");
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
			}else if(level==1){
				session.setAttribute("photo", "free_photo2");
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			}
			view.forward(request, response);
			
		}
		else if(action.equals("pt_free_list")){
			String mem_nick=request.getParameter("nick");
			System.out.println("nick"+mem_nick);
			int level=photoFreeDAO.young_level(mem_nick);
			
			String searchword=request.getParameter("searchword");
			String search=request.getParameter("search");
			ArrayList list2 =photoFreeDAO.pt_free_recom();
			ArrayList list3 =photoFreeDAO.pt_free_bestview();
			photoFreeBean=photoFreeDAO.pt_free_recom_top();
			if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			param.put("pageSize", 8 );
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
			int endNum = pageNo + (Integer)param.get("pageSize");
			param.put( "startNum", pageNo );
			param.put("searchword", searchword);
			param.put("search",search);
			//param.put( "recordCount", (Integer)param.get("pageSize") );
			ArrayList list =photoFreeDAO.pt_free_list2(param);
			int totalCount = photoFreeDAO.pfTotalCount(param);
			param.put("totalCount", totalCount);
			paging=new common.PagingManager();
			paging.setPageNo(param);
			
			request.setAttribute("list", list);
			request.setAttribute("list2", list2);
			request.setAttribute("list3", list3);
			request.setAttribute("photoFreeBean", photoFreeBean);
			request.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page","/photo/PtFree/PT_Free_List");
			
			if(level==2){
				request.setAttribute("photo", "free_photo");
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
			}else if(level==1){
				request.setAttribute("photo", "free_photo2");
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			
			}else if(level==3){
				request.setAttribute("photo", "free_photo3");
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
			}
			view.forward(request, response);
			
		}else if(action.equals("pt_free_read")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			int id=Integer.parseInt(request.getParameter("id"));
			photoFreeBean=photoFreeDAO.pt_free_read(id);
			ArrayList list=photoFreeDAO.pt_free_commentr(id);
			
			request.setAttribute("list", list);
			request.setAttribute("photoFreeBean", photoFreeBean);
			request.setAttribute("page","/photo/PtFree/PT_Free_Read");
			if(level==2){
				
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
			}else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			
			}else if(level==3){
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
			}
			view.forward(request, response);
			
			//request.setAttribute("photoFreeBean", photoFreeBean);
			//request.setAttribute("list", list);
			//request.getRequestDispatcher("/photo/PtFree/PT_Free_Read.jsp").forward(request, response);
		}else if(action.equals("pt_free_write_form")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			session = request.getSession();
			request.setAttribute("page","/photo/PtFree/PT_Free_Write");
			if(level==2){
				
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
			}else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			}
			view.forward(request, response);
		}else if(action.equals("pt_free_write")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			System.out.println("왜!!!!!1");
			 String po_contents=request.getParameter("po_contents");
			 //po_contents=po_contents.replaceAll("=", "&#61;"); 
			 StringTokenizer st1=new StringTokenizer(po_contents,"=");  			 
			 String a=st1.nextToken();
			 String b=st1.nextToken();
			 String c=st1.nextToken();
			 String d=st1.nextToken();
			 //String e=st1.nextToken();
			 String f=null;
			 if(d!=null){
				 StringTokenizer st2=new StringTokenizer(d," ");  
				 f=st2.nextToken();
			 }
			photoFreeBean=new PhotoFreeBean();
			photoFreeBean.setPo_Nick(request.getParameter("po_nick"));
			photoFreeBean.setPo_Title(request.getParameter("po_title"));
			photoFreeBean.setPo_Contents(request.getParameter("po_contents"));
			photoFreeBean.setPhoto(f);
			photoFreeDAO.pt_free_write(photoFreeBean);

			out.println( "<script>" );
			//out.println( "alert(\"완료.\");" );
			out.println( "location.href=\"/KOMORIUTA/PhotoFreeAction.do?action=pt_free_list2&nick="+mem_nick+"\";");
			out.println( "</script>" ); 
			
			
			//session = request.getSession();
			//request.setAttribute("page","/photo/PtFree/PT_Free_List");
			//view = request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_list");
			//view.forward(request, response);
			//request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_list").forward(request, response);
		}else if(action.equals("pt_free_modify_form")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			int id=Integer.parseInt(request.getParameter("id"));
			photoFreeBean=photoFreeDAO.pt_free_read(id);
			
			session = request.getSession();
			session.setAttribute("photoFreeBean", photoFreeBean);
			request.setAttribute("page","/photo/PtFree/PT_Free_Modify");
			if(level==2){
				
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
			}else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			}
			view.forward(request, response);
			
			//request.setAttribute("photoFreeBean", photoFreeBean);
			//request.getRequestDispatcher("/photo/PtFree/PT_Free_Modify.jsp").forward(request, response);
		}else if(action.equals("pt_free_modify")){
			 String mem_nick=request.getParameter("nick");
			 int level=photoFreeDAO.young_level(mem_nick);
			 String po_contents=request.getParameter("po_contents");
			 StringTokenizer st1=new StringTokenizer(po_contents,"=");  			 
			 String a=st1.nextToken();
			 String b=st1.nextToken();
			 String c=st1.nextToken();
			 String d=st1.nextToken();
			 String f=null;
			 if(d!=null){
				 StringTokenizer st2=new StringTokenizer(d," ");  
				 f=st2.nextToken();
			 }
			photoFreeBean=new PhotoFreeBean();
			int id=Integer.parseInt(request.getParameter("id"));
			photoFreeBean.setID(id);
			photoFreeBean.setPo_Title(request.getParameter("po_title"));
			photoFreeBean.setPo_Contents(request.getParameter("po_contents"));
			photoFreeBean.setPhoto(f);
			photoFreeDAO.pt_free_modify(photoFreeBean);
			out.println( "<script>" );
			//out.println( "alert(\"완료.\");" );
			out.println( "location.href=\"/KOMORIUTA/PhotoFreeAction.do?action=pt_free_list2&nick="+mem_nick+"\";" );
			out.println( "</script>" ); 
			//request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_list").forward(request, response);
		}else if(action.equals("pt_free_delete")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			int id=Integer.parseInt(request.getParameter("id"));
			photoFreeDAO.pt_free_delete(id);
			out.println( "<script>" );
			//out.println( "alert(\"완료.\");" );
			out.println( "location.href=\"/KOMORIUTA/PhotoFreeAction.do?action=pt_free_list2&nick="+mem_nick+"\";" );
			out.println( "</script>" ); 
			//request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_list").forward(request, response);
		}else if(action.equals("pt_free_comment")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			photoCommentBean=new PhotoCommentBean();
			int id=Integer.parseInt(request.getParameter("photozon_id"));
			String photocom_nick=request.getParameter("photocom_nick");
			String photocom_contents=request.getParameter("photocom_contents");
			
			photoFreeDAO.pt_free_comment(id,photocom_nick,photocom_contents);
			request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_read&id="+id).forward(request, response);
		}else if(action.equals("pt_free_recom")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			int id=Integer.parseInt(request.getParameter("id"));
			photoFreeDAO.pt_free_recom(id);
			request.getRequestDispatcher("/PhotoFreeAction.do?action=pt_free_read&id="+id).forward(request, response);
		}else if(action.equals("pt_free_com_cancle")){
			String mem_nick=request.getParameter("nick");
			int level=photoFreeDAO.young_level(mem_nick);
			int id=Integer.parseInt(request.getParameter("id"));
			int com_id=Integer.parseInt(request.getParameter("com_id"));
			photoFreeDAO.pt_free_com_cancle(com_id);
			out.println( "<script>" );
			//out.println( "alert(\"완료.\");" );
			out.println( "location.href=\"/KOMORIUTA/PhotoFreeAction.do?action=pt_free_read&id="+id+"&nick="+mem_nick+"\";" );
			out.println( "</script>" ); 
		}
		
	}

}
